import QtQuick 1.1
import QtMobility.sensors 1.1
import com.nokia.meego 1.0
import "scripts/SnowballLogic.js" as SnowballLogic
import Qt.labs.gestures 1.0
import Qt 4.7


Page {

    id:gamePage
    lockInLandscape: true
    orientationLock: PageOrientation.LockLandscape

    Rectangle {
        id: screen
        width: 854
        height: 480
        Component.onCompleted: {SnowballLogic.gameStart(screen, snowman);}

        Image {
            /*id: background*/
            source: "images/GameBg.png"
            x: 0
            y: 0
        }

        MouseArea {
            anchors.fill: parent
            onMousePositionChanged: {
                console.log("Mouse changed");
            }

            onClicked: {
                console.log("SNOWBALL!");

            }
        }

        Image {
            id: snowman
            x: 50
            y: 360
            source: "images/snowman.png"
        }

        Image {
            id: enemySnowman
            x: 700
            y: 360
            source: "images/snowman_enemy.png"
        }

        GestureArea
        {
            anchors.fill: parent
            onSwipe:{
                console.log(gesture.swipeAngle);
            }
            onTap: {
                //console.log(gesture.position.x + " " + gesture.position.y);
            }
            onTapAndHold:{
                console.log(gesture.position.x + " " + gesture.position.y);
            }

            onPan: {
                //console.log("pan " + gesture.acceleration);
            }
        }

        PinchArea
        {
            anchors.fill: parent

            onPinchUpdated: {
                if(pinch.point1.x != pinch.point2.x && pinch.point1.y != pinch.point2.y)
                {
                    SnowballLogic.setLastThrow(pinch);
                }
            }

            onPinchFinished: {
                SnowballLogic.ThrowBall(screen);
            }
        }



        /*SoundEffect {
             id: splashSound
             source: "sounds/ice-block-drop.wav"
         }*/


        Accelerometer  {
            id: accel
            active: true
            onReadingChanged: {
                //console.log("x="+reading.x + "\n y=" + reading.y +"\n z= "+ reading.z);
                SnowballLogic.moveSnowman(reading);
            }
        }

        Timer {
            id: myTimer;
            interval: 40;
            repeat: true;
            running: true;
            onTriggered: {
                SnowballLogic.timeTick();
            }
        }


        Timer {
            id: scoreUpdateTimer;
            interval: 1000;
            repeat: true;
            running: true;
            onTriggered: {
                SnowballLogic.updateScoreInformation();
            }
        }

        Label {
            id: label1
            x: 21
            y: 42
            width: 54
            height: 25
            text: "Lifes"
            font.pointSize: 12
            font.bold: false
        }

        Label {
            id: lblLifesCount
            x: 85
            y: 42
            width: 37
            height: 26
            text: "5"
        }

        Label {
            id: label2
            x: 600
            y: 43
            width: 134
            height: 25
            text: "Enemy lifes"
            font.pointSize: 12
            font.bold: false
        }

        Label {
            id: lblEnemyLifesCount
            x: 762
            y: 42
            width: 37
            height: 26
            text: "5"
        }
    }

    // Signal handler for network signals
    Item {
        Connections {
            target: BluetoothManager
            onUpdatePositionEvent: {
                enemySnowman.x = positionX;
            }

            onThrowBallEvent: {
                SnowballLogic.ThrowEnemyBall(positionX, positionY, speedX, speedY);
            }

            onUpdateScoreEvent: {
                console.log("Received score update!" + myScore + enemyScore);
                SnowballLogic.setScoreInfo(myScore, enemyScore);
                SnowballLogic.checkScores();
            }

            onServerModeSet: {
                SnowballLogic.setServerMode();
            }
        }
    }
}

